<template>
  <div class="login-container">
    <div class="login-background"></div>

    <div class="login-form-container">
      <el-form 
      ref="loginForm" 
      :model="loginForm" 
      label-width="0" 
      class="login-form" 
      :rules="rules">
        <p class="login-title">用户登录</p>

        <el-form-item prop="userName">
          <el-input v-model="loginForm.userName" placeholder="用户名" prefix-icon="el-icon-user"  class="transparent" ></el-input>
        </el-form-item>

        <el-form-item prop="password">
          <el-input type="password" v-model="loginForm.password" placeholder="密码" prefix-icon="el-icon-lock"  class="transparent"
          show-password auto-complete="off" size="small" @keyup.enter.native="login"></el-input>
        </el-form-item>

         <el-form-item  prop="rememberMe">
            <el-checkbox v-model="loginForm.rememberMe" style="color: white;">记住我</el-checkbox>
          </el-form-item>

        <el-form-item>
          <el-button type="primary" @click="login" :disabled="confirm_disabled" :loading="loginLoading" class="login-button">登录</el-button>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>

<script>
export default {
  data() {
    return {
      confirm_disabled: false,
      loginForm:{
        userName: '',
        password: '',
        rememberMe: false
      },
      rules:{
        userName: [
            { required: true, message: '请输入用户名', trigger: 'blur' },
            // { min: 1, max: 5, message: '长度在 1 到 5 个字符', trigger: 'blur' }
          ],
        password: [
            { required: true, message: '请输入密码', trigger: 'blur' },
            // { min: 1, max: 5, message: '长度在 1 到 5 个字符', trigger: 'blur' }
          ],
      },
      loginLoading: false
    };
  },
  methods: {
    login() {
      // 模拟登录
      this.loginLoading = true;
      // this.confirm_disabled = true;
      setTimeout(() => {
        // 登录成功后的逻辑，这里仅演示跳转至首页   
      this.$refs.loginForm.validate((valid) => {
        if(valid){ // valid成功为true，失败时为fasle
          // 去后台验证用户名密码
          this.$axios.post("/api/user/login",this.loginForm).then(res => res.data).then(res => {
            console.log(res);
            if(res.code == 200){
              sessionStorage.setItem("user",JSON.stringify(res.data))
              // 跳转到主页
              this.$router.replace("/")
            }else{
              this.confirm_disabled = false;
              this.$message({
                type:"error",
                message:"校验失败，用户名或密码错误"
              })
              // alert('校验失败，用户名或密码错误');
              return false;
            }
          })
          
        }else{
          this.confirm_disabled = false;
          console.log('校验失败');
          return false;
        }
      })
      }, 1500);
    }
  }
};
</script>

<style scoped>
.login-container {
  display: flex;
  justify-content: center;
  align-items: center;
  height: 100vh;
  background-color: #f0f2f5;
  
}

.login-background {
  /* position: relative; */
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
   /* 设置背景图片 */
  background: url('../assets/login.gif') center center / cover no-repeat; 
 
  /* filter: blur(5px); */
}

.login-form-container {
  background-color: rgba(0, 0, 0, 0.1);
  position: absolute;
  width: 320px;
  /* background-color: blue; */
  /* border: 1px solid blue; */
  border-radius: 8px;
  border: 0.5px dashed #336699;
  padding: 40px;
  box-shadow: 0 4px 16px rgba(0, 0, 0, 0.1);
  
}


.login-title {
  text-align: center;
  margin-bottom: 24px;
  font-size: 24px;
  color: white;
}

.login-form {
  width: 100%;
}

.login-button {
  width: 100%;
}

.transparent{
  color:#336699
}

</style>
